home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1997 July: Mac OS SDK / Dev.CD Jul 97 SDK2.toast / Development Kits (Disc 2) / ScriptX / Documentation / Code Examples from Docs / compguid / objsystk / plrpoint.sx < prev   
Encoding:
Text File  |  1996-05-21  |  5.1 KB  |  203 lines  |  [TEXT/ttxt]

  1. -- <<<-
  2.  
  3. -- plrpoint.sx
  4. -- by Howard Metzenberg, with help from Ross Nelson
  5. -- ScriptX sample code
  6. -- the PolarPoint and CartesianPoint classes
  7.  
  8. -- this example demonstrates how to implement
  9. -- several of the common protocols for ScriptX objects
  10.  
  11.  
  12. module ObjectSystemKernelTest1 uses ScriptX end
  13. in module ObjectSystemKernelTest1
  14.  
  15. class PolarPoint ()
  16.     instance variables
  17.         r, _theta
  18.     instance methods
  19.     method init self #rest args #key r: theta: -> (
  20.         if not isAKindOf r Number do
  21.             report invalidNumber r
  22.         if r < 0 do
  23.             report badParameter \
  24.                 #(r, init, self, "r must be > 0")
  25.         if not isAKindOf theta Number do
  26.             report invalidNumber theta
  27.         apply nextMethod self args
  28.     )
  29.     method afterInit self #rest args #key r: theta: -> (
  30.         apply nextMethod self args
  31.         self.r := r
  32.         self.theta := theta
  33.     )
  34.     method thetaGetter self -> self._theta
  35.     -- normalizes theta between 0 and (2 * pi)
  36.     method thetaSetter self value -> (
  37.         local constant twoPI := (2 * pi)
  38.         if value >= twoPI then
  39.             self._theta := mod value twoPI
  40.         else if value < 0 then
  41.             self._theta := mod (twoPI + value) twoPI
  42.         else
  43.             self._theta := value
  44.     )
  45. end
  46.  
  47. class method newFrom self {class PolarPoint} other -> (
  48.     if (isAKindOf other Point) then
  49.         new self r:(sqrt (other.x * other.x + other.y * other.y)) \
  50.             theta:(atan (other.y/other.x))
  51.     else
  52.         report cantCoerce #(other, self)
  53. )
  54.  
  55. method morph self {class PolarPoint} cls style -> (
  56.     if not isAKindOf cls Behavior do
  57.         report badParameter \
  58.             #(cls, morph, PolarPoint, "Expecting name of Class")
  59.     if (isSub cls String) then
  60.         "(" + (self.r as String) + " x " + (self.theta as String) + ")"
  61.     else if (isSub cls Point) then
  62.         new cls x:(self.r * (sin self.theta)) \
  63.             y:(self.r * (cos self.theta))
  64.     else
  65.         report cantCoerce #(self, cls)
  66. )
  67.  
  68. -- test coercion on the PolarPoint class
  69. object myPoint (Point) x:6, y:6 end
  70. object myPolarPoint (PolarPoint) r:(6 * sqrt 2), theta:(pi/4) end
  71. myPolarPoint.r
  72. myPolarPoint.theta
  73. global convertedPoint := myPoint as PolarPoint
  74. convertedPoint.r = (6 * sqrt 2)
  75. convertedPoint.theta = (pi / 4)
  76. myPolarPoint as String
  77.  
  78. -- implement the Comparison protocol
  79. method isComparable self {class PolarPoint} other -> (
  80.     if isAKindOf other Point or isAKindOf other PolarPoint then
  81.         true
  82.     else
  83.         false
  84. )
  85.  
  86. method localEqual self {class PolarPoint} other -> (
  87.     local comparator
  88.     if getClass other = PolarPoint then
  89.         comparator := other
  90.     else if isAKindOf other Point then
  91.         comparator := other as PolarPoint
  92.     else -- pass it on to RootObject, which reports exception
  93.         nextMethod self other
  94.     if self.r = comparator.r and self.theta = comparator.theta then
  95.         return true
  96.     else
  97.         return false
  98. )
  99.  
  100. method localLt self {class PolarPoint} other -> (
  101.     local comparator := undefined
  102.     if getClass other = PolarPoint then -- the same class
  103.         comparator := other
  104.     else if isAKindOf other Point then -- a comparable class
  105.         comparator := other as PolarPoint
  106.     else
  107.         report generalError \
  108.             "Cannot compare PolarPoint and %* objects." \
  109.             (getClassName other)
  110.     -- now do the actual comparison
  111.     if self.r < comparator.r then 
  112.         return true
  113.     else if self.r > comparator.r then
  114.         return false
  115.     else (
  116.          if self.theta < comparator.theta then 
  117.              return true
  118.          else 
  119.              return false
  120.     )
  121. )
  122.  
  123. --
  124. -- now create the CartesianPoint class
  125. -- and implement the Comparison protocol for it
  126. --
  127. class CartesianPoint (Point) end
  128.  
  129. method isComparable self {class CartesianPoint} other -> (
  130.     if isAKindOf other Point or isAKindOf other PolarPoint then
  131.         true
  132.     else
  133.         nextMethod self other
  134. )
  135.  
  136. method localEqual self {class CartesianPoint} other -> (
  137.     if getClass other = PolarPoint then
  138.         localEqual other self
  139.     else if isAKindOf other Point then
  140.         if self.x = other.x and self.y = other.y then
  141.             true
  142.         else
  143.             false
  144.     else
  145.         nextMethod self other
  146. )
  147.  
  148. method localLt self {class CartesianPoint} other -> (
  149.     local comparator := undefined
  150.     local cls := getClass other
  151.     if cls = PolarPoint then
  152.         localLt other self
  153.     else if isAKindOf other Point then
  154.         comparator := other as PolarPoint
  155.     else
  156.         report generalError "Cannot compare CartesianPoint and %*." cls
  157.     if self.r < comparator.r then 
  158.         return true
  159.     else if self.r > comparator.r then
  160.         return false
  161.     else (
  162.          if self.theta < comparator.theta then 
  163.              return true
  164.          else 
  165.              return false
  166.     )
  167. )
  168.  
  169. -- implement prin for PolarPoint
  170. --
  171. method prin self {class PolarPoint} formatStyle printStream -> (
  172.     case formatStyle of
  173.         @normal:(
  174.             format printStream "[%n1, %n2] as PolarPoint" \
  175.                 #(self.r, self.theta) 
  176.         )
  177.         @complete:(
  178.             format printStream "[r=%c1, theta=%c2] as PolarPoint" \
  179.                 #(self.r, self.theta) 
  180.         )
  181.         @unadorned:(
  182.             format printStream "[%u1, %u2]" #(self.r, self.theta) 
  183.         ) 
  184.         @debug:(
  185.             (methodBinding RootObject prin) self @normal printStream
  186.             writeString printStream ": ["
  187.             writeString printStream (getClassName self.r)
  188.             writeString printStream ": "
  189.             writeString printStream (self.r as String)
  190.             writeString printStream ", "
  191.             writeString printStream (getClassName self.theta)
  192.             writeString printStream ": "
  193.             writeString printStream (self.theta as String)
  194.             writeString printStream "]"
  195.         ) 
  196.         otherwise: (
  197.             nextMethod self formatStyle printStream
  198.         )
  199.     end
  200. )
  201.  
  202. -- >>>
  203.